/*
package com.io.webflow.service.impl;

import com.io.model.AsyncCallContextData;
import com.io.model.BaseModel;
import com.io.model.TerminalPrincipal;
import com.io.nettyflow.rounter.ContextManager;
import com.io.webflow.service.TaskExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

*/
/**
 * Created by yangyx on 2016/8/2.
 *//*

@Service
public class TaskExecutorImpl implements TaskExecutor{

    private int DefaultRetryTimes = 3; //默认重试次数3次
    private int DefaultTimeOut = 10; //默认超时10秒

    private static final Logger logger= LoggerFactory.getLogger(TaskExecutorImpl.class);

    @Autowired
    private ContextManager contextManager;

    private ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);

    public void scheduleSendToTerminal(String tid, BaseModel model) {
        scheduleSendToTerminal(tid, model, DefaultTimeOut, DefaultRetryTimes, null, null);


    }
    public void scheduleSendToTerminal(String tid, BaseModel model, Object extraData, SendToTerminalTimeoutCallback cb) {
        scheduleSendToTerminal(tid, model, DefaultTimeOut, DefaultRetryTimes, extraData, cb);
    }

    public void scheduleSendToTerminal(String tid, BaseModel model, int timeout, int retryTimes, Object extraData, SendToTerminalTimeoutCallback cb){
        //检查此tid是否连着,如果没有,则不能继续执行
        scheduler.submit(new SendToTerminalTask(tid, model, timeout, retryTimes, extraData, cb));
    }
    //内部接口
    public interface SendToTerminalTimeoutCallback{
        void onTimeOut(String tid, BaseModel toSend, Object extraData);
    }

    //内部线程类
    class SendToTerminalTask implements Runnable{
        private String _tid;
        private BaseModel _model;
        private int _timeout;
        private int _retryTimes;
        private Object _extraData;
        private SendToTerminalTimeoutCallback _cb;
        private int _triedTimes; //已经尝试的次数
        public SendToTerminalTask(String tid, BaseModel model, int timeout, int retryTimes, Object extraData, SendToTerminalTimeoutCallback cb){
            _tid = tid;
            _model = model;
            _timeout = timeout;
            _retryTimes = retryTimes;
            _extraData = extraData;
            _cb = cb;
            _triedTimes = 0;
        }
        private void send(TerminalPrincipal principal){
            principal.ctx.write(_model);
            _triedTimes++;
            principal.setAccdObj(_model.getTag(), new AsyncCallContextData());
        }
        public void run() {{
            TerminalPrincipal principal = contextManager.getPrincipalByTid(_tid);
            if(principal!=null){
                //获取总尝试次数
                if(_triedTimes==0){     //第一次调用
                    send(principal);
                    if (_timeout>0) {   //N秒后做超时检查
                        scheduler.schedule(this, _timeout, TimeUnit.SECONDS);
                    }
                }
                else{                   //超时检查
                    AsyncCallContextData accd = principal.popAccdObj(_model.getTag());
                    if(accd!=null){     //出现超时
                        if(_triedTimes>=_retryTimes){ //次数超过限制了,不用再试了
                            if(_cb!=null){
                                _cb.onTimeOut(_tid, _model, _extraData);
                            }
                        }
                        else{
                            send(principal);
                            scheduler.schedule(this, _timeout, TimeUnit.SECONDS);
                        }
                    }
                }
                principal.ctx.flush();
            }
        }
        }
    }

}
*/
